package com.ruoyi.park.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.park.domain.WyLampManagement;
import com.ruoyi.park.domain.vo.BuiRoomVO;
import com.ruoyi.park.domain.vo.WyLampManagementVO;
import com.ruoyi.park.service.IWyLampManagementService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 路灯管理Controller
 *
 * @author cjie
 * @date 2021-07-22
 */
@Api(tags = " 路灯管理")
@RestController
@RequestMapping("/wyLampManagement")
public class WyLampManagementController extends BaseController {
    @Autowired
    private IWyLampManagementService wyLampManagementService;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询路灯管理列表
     */
    @ApiOperation(value="查询路灯管理列表", notes="查询路灯管理列表")
    @PreAuthorize(hasPermi = "park:wyLampManagement:list")
    @GetMapping("/list")
    public TableDataInfo list(WyLampManagement wyLampManagement) {
        startPage();
        List<WyLampManagement> list = wyLampManagementService.selectWyLampManagementList(wyLampManagement);
        return getDataTable(list);
    }

    /**
     * 导出路灯管理列表
     */
    @ApiOperation(value="导出路灯管理列表", notes="导出路灯管理列表")
    @PreAuthorize(hasPermi = "park:wyLampManagement:export")
    @Log(title = "路灯管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyLampManagement wyLampManagement) throws IOException {
        List<WyLampManagement> list = wyLampManagementService.selectWyLampManagementList(wyLampManagement);
        ExcelUtil<WyLampManagement> util = new ExcelUtil<WyLampManagement>(WyLampManagement.class);
        util.exportExcel(response, list, "路灯管理数据");
    }

    /**
     * 获取路灯管理详细信息
     */
    @ApiOperation(value="获取路灯管理详细信息", notes="获取路灯管理详细信息")
    @PreAuthorize(hasPermi = "park:wyLampManagement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(wyLampManagementService.selectWyLampManagementById(id));
    }

    /**
     * 新增路灯管理
     */
    @ApiOperation(value="新增路灯管理", notes="新增路灯管理")
    @PreAuthorize(hasPermi = "park:wyLampManagement:add")
    @Log(title = "路灯管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyLampManagement wyLampManagement) {
//        remoteSysBmCode.setCode(wyLampManagement.getDeviceNum());
//        String code=  remoteBmCodeService.insertBmCode(remoteSysBmCode);
//        wyLampManagement.setDeviceNum(code);
        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(wyLampManagement.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        remoteSysBmType.getBmId();//编码类型Id
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        wyLampManagement.setDeviceNum(nowCode);
        wyLampManagement.setDeviceType(deviceType);
        return toAjax(wyLampManagementService.insertWyLampManagement(wyLampManagement));
    }

    /**
     * 修改路灯管理
     */
    @ApiOperation(value="修改路灯管理", notes="修改路灯管理")
    @PreAuthorize(hasPermi = "park:wyLampManagement:edit")
    @Log(title = "路灯管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyLampManagement wyLampManagement) {
//        RemoteSysBmCode remoteSysBmCode=new RemoteSysBmCode();
//        remoteSysBmCode.setCode(wyLampManagement.getDeviceNum());
//        String code=  remoteBmCodeService.insertBmCode(remoteSysBmCode);
//        System.out.println(code);
//        System.out.println(remoteSysBmCode.toString());
        return toAjax(wyLampManagementService.updateWyLampManagement(wyLampManagement));
    }

    /**
     * 删除路灯管理
     */
    @ApiOperation(value="删除路灯管理", notes="删除路灯管理")
    @ApiImplicitParam(name = "ids", value = "路灯管理ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "park:wyLampManagement:remove")
    @Log(title = "路灯管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(wyLampManagementService.deleteWyLampManagementByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<WyLampManagementVO> util = new ExcelUtil<WyLampManagementVO>(WyLampManagementVO.class);
        util.importTemplateExcel(response, "房源数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @ApiOperation(value = "导入路灯数据", notes = "导入路灯数据")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<WyLampManagementVO> util = new ExcelUtil<WyLampManagementVO>(WyLampManagementVO.class);
        List<WyLampManagementVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = wyLampManagementService.importData(buiRoomVOList);
        return AjaxResult.success(message);

    }

}
